<?php
/**
 * Created by PhpStorm.
 * User: 阿沁
 * Date: 2024/4/10
 * Time: 下午4:41
 * SOURCE: 中国大陆工时规管现况（标准工时）：
 *              一天工作时间为8小时，平均每周工时不超过40小时;
 *              加班上限为一天3小时及一个月36小时，逾时工作薪金不低于平日工资的150％。
 *              而一周最高工时则为48小时。平均每月计薪天数为21.75天。
 * TO: 我是阿沁,健康撸代码,拒绝通宵。
 * 版权所有: © 2024-2030 阿沁
 * WARNING: 未经本人许可, 禁止一切商用
 * CLASS: < VOD.php >
 */

namespace app\common;

use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadAttachedMediaRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadImageRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadVideoRequest;
use AlibabaCloud\SDK\Vod\V20170321\Vod;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;

class Upload
{
	/**
	 * 获取视频媒资授权
	 * @throws \Exception
	 */
	public static function createUploadVideo($coverURL = '', $fileName = '', $title = '', $userData = [])
	{
		$client = self::createClient();
		$createUploadVideoRequest = new CreateUploadVideoRequest([
			'coverURL' => $coverURL,
			"fileName" => $fileName,
			"title" => $title,
			'userData' => json_encode($userData),#MessageCallback CallbackType CallbackURL
			'storageLocation' => 'outin-a4d5c2f2f6e511ee90d900163e16852e.oss-cn-shanghai.aliyuncs.com'
		]);
		$runtime = new RuntimeOptions([]);
		try {
			// 复制代码运行请自行打印 API 的返回值
			$ret = $client->createUploadVideoWithOptions($createUploadVideoRequest, $runtime);
		} catch (\Throwable $e) {
			throw new \Exception($e->getMessage());
		}
		return $ret->body;
	}
	
	/**
	 * 获取图片媒资授权
	 * @throws \Exception
	 */
	public static function createUploadImage($userData)
	{
		$client = self::createClient();
		$createUploadImageRequest = new CreateUploadImageRequest([
			'imageType'=>'default',
			'storageLocation' => 'outin-a4d5c2f2f6e511ee90d900163e16852e.oss-cn-shanghai.aliyuncs.com',
			'userData' => json_encode($userData),#MessageCallback CallbackType CallbackURL
		]);
		$runtime = new RuntimeOptions([]);
		try {
			// 复制代码运行请自行打印 API 的返回值
			$ret = $client->createUploadImageWithOptions($createUploadImageRequest, $runtime);
		} catch (\Throwable $e) {
			throw new \Exception($e->getMessage());
		}
		return $ret->body;
	}
	
	/**
	 * 获取上传视频媒资授权
	 * @throws \Exception
	 */
	public static function createUploadAttachedMedia($businessType, $mediaExt)
	{
		$client = self::createClient();
		$createUploadAttachedMediaRequest = new CreateUploadAttachedMediaRequest([
			"businessType" => $businessType,
			"mediaExt" => $mediaExt,
			'storageLocation' => 'outin-a4d5c2f2f6e511ee90d900163e16852e.oss-cn-shanghai.aliyuncs.com',
		]);
		$runtime = new RuntimeOptions([]);
		try {
			// 复制代码运行请自行打印 API 的返回值
			$client->createUploadAttachedMediaWithOptions($createUploadAttachedMediaRequest, $runtime);
		}
		catch (\Exception $e) {
			throw new \Exception($e->getMessage());
		}
	}
	
	/**
	 * 使用AK&SK初始化账号Client
	 */
	private static function createClient()
	{
		// 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
		// 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/311677.html。
		$config = new Config([
			// 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
			"accessKeyId" => env("ALIBABA_CLOUD_ACCESS_KEY_ID"),
			// 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
			"accessKeySecret" => env("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
		]);
		// Endpoint 请参考 https://api.aliyun.com/product/vod
		$config->endpoint = "vod.cn-shanghai.aliyuncs.com";
		return new Vod($config);
	}
	
}